<template>
    <a-modal
            title="修改密码"
            :visible="visible"
            :width="'600px'"
            @ok="handleOk"
            @cancel="cancel"
            :confirmLoading="confirmLoading"
            :maskClosable="false"
    >
        <a-form-model ref="form" :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
            <a-form-model-item
                    label="旧密码"
                    prop="oldPassword"
                    :rules="{
                        required: true,
                        message: '请输入旧密码',
                        trigger: 'change',
                    }">
                <a-input-password
                        placeholder="请输入旧密码"
                        v-model="form.oldPassword">
                </a-input-password>
            </a-form-model-item>
            <a-form-model-item
                    label="新密码"
                    prop="newPassword"
                    :rules="{
                        required: true,
                        message: '请输入新密码',
                        trigger: 'change',
                    }">
                <a-input-password
                        placeholder="请输入新密码"
                        v-model="form.newPassword">
                </a-input-password>
            </a-form-model-item>
            <a-form-model-item
                    label="确认密码"
                    prop="confirmPassword"
                    :rules="{
                        required: true,
                        message: '请输入确认密码',
                        trigger: 'change',
                    }">
                <a-input-password
                        placeholder="请输入确认密码"
                        v-model="form.confirmPassword">
                </a-input-password>
            </a-form-model-item>
        </a-form-model>
    </a-modal>
</template>

<script>
    import {password_modify} from "../../assets/api";
    import md5 from 'md5'
    import {getToken, removeToken} from "../../assets/service/auth";
    export default {
        name: "index",
        data(){
            return{
                labelCol: { span: 4 },
                wrapperCol: { span: 20 },
                visible:false,
                confirmLoading:false,
                form:{}
            }
        },
        props:{
            mustSet:{
                type:Boolean,
                default:false
            },
            token:{
                type:String,
                default:getToken()
            }
        },
        methods:{
            cancel(){
                if(this.mustSet){
                    this.$message.error('密码必须修改哦')
                    this.visible = false
                    removeToken()
                }else{
                    this.visible = false
                }
            },
            handleOk(){
                this.$refs.form.validate(valid => {
                    if (valid) {
                        let formData = Object.assign({},this.form)
                        formData.oldPassword = md5(formData.oldPassword)
                        formData.newPassword = md5(formData.newPassword)
                        formData.confirmPassword = md5(formData.confirmPassword)
                        console.log(this.token)
                        password_modify({
                            param:formData,
                            token:this.token
                        })
                            .then(result=>{
                                this.$message.success('修改成功')
                                this.visible = false
                                if(this.mustSet){
                                    this.$emit('passwordGo')
                                }
                            })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            }
        }
    }
</script>

<style scoped>

</style>
